package com.example.demoofimagedownloadapp.view.utils;



import android.animation.ValueAnimator;
import android.os.Build;
import android.util.ArrayMap;
import android.util.Log;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.LinearInterpolator;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;

import androidx.annotation.RequiresApi;

import com.example.demoofimagedownloadapp.R;

import java.util.ArrayList;
import java.util.List;

/**
 * @author ts
 */
public class AnimationHelper {
    private static final String TAG = AnimationHelper.class.getName();
    private List<ImageView> mImageList = new ArrayList<>();
    private static final int ANIMATION_RUNTIME = 1200;

    private View mView;
    int [] axiosA = new int[2];
    int [] axiosB = new int[2];
    float [] mViewSizeA = new float[2];
    float [] mViewSizeB = new float[2];

    int[] location = new int[2];
    int[] location_base_left = new int[2];
    int[] guidelineX = new int[2];
    int[] guidelineY = new int[2];

    private ArrayMap mViewMap;
    public AnimationHelper(ArrayMap arrayMap){
        mViewMap = arrayMap;
        Log.d(TAG, "AnimationHelper: "+arrayMap);

        mImageList.add((ImageView) getView(R.id.image_hello));
        mImageList.add((ImageView) getView(R.id.image_area2));
        mImageList.add((ImageView) getView(R.id.image_base_left));
        mImageList.add((ImageView) getView(R.id.image_base_center));
    }

    synchronized public void setAnimation() {
        initParams();
        ValueAnimator valueAnimator = ValueAnimator.ofFloat(0,1.f);
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator valueAnimator) {
                float progress = (float) valueAnimator.getAnimatedValue();
//                Log.d(TAG, "onAnimationUpdate: "+progress);

                float offsetX_center = (location[0]-location_base_left[0])*progress;
                float offsetY_center = (location[1]-location_base_left[1])*progress;

//                Log.d(TAG, "onAnimationUpdate: "+offsetX_center+" "+location_base_left[0]+offsetX_center);
                Log.d(TAG, "onAnimationUpdate: "+offsetX_center+" "+location_base_left[1] + " "+offsetY_center);
                mImageList.get(1).setTranslationX(offsetX_center);
                mImageList.get(1).setTranslationY(offsetY_center);


                if(progress > 0.25f){
                    mImageList.get(1).setZ(20.f);
                    mImageList.get(0).setZ(10.f);
                }
            }
        });

        valueAnimator.setDuration(ANIMATION_RUNTIME);
        valueAnimator.start();
    }

    synchronized public void setAnimaToChange(){
        initParams();
        mImageList.get(1).setZ(30.f);
        AnimationSet set_1 = new AnimationSet(true);
        AnimationSet set_2 = new AnimationSet(true);

        set_1.addAnimation(new ScaleAnimation(
                1.0f,
                mViewSizeB[0]/mViewSizeA[0],
                1.0f,
                mViewSizeB[1]/mViewSizeA[1],
                ScaleAnimation.RELATIVE_TO_PARENT,
                ScaleAnimation.RELATIVE_TO_PARENT
        ));
        set_1.addAnimation(new TranslateAnimation(
                0,
                axiosA[0]-location[0],
                0,
                axiosA[1]-location[1]
        ));

        set_1.setInterpolator(new LinearInterpolator());
        set_1.setFillEnabled(true);

        set_1.setFillAfter(true);
        set_1.setDuration(ANIMATION_RUNTIME/2);
        mImageList.get(0).startAnimation(set_1);
        set_1.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
                int[] temp = new int[2];
                mImageList.get(0).getLocationInWindow(temp);
                Log.d(TAG, "onAnimationStart: "+temp[0]+" "+temp[1]);
            }

            @RequiresApi(api = Build.VERSION_CODES.Q)
            @Override
            public void onAnimationEnd(Animation animation) {
                mImageList.get(0).clearAnimation();
                int[] temp = new int[2];

//                mImageList.get(0).setX(axiosA[0]);
//                mImageList.get(0).setY(mImageList.get(0).getTop());
                mImageList.get(0).layout(
                        mImageList.get(2).getLeft(),
                        mImageList.get(2).getTop(),
                        mImageList.get(2).getRight(),
                        mImageList.get(2).getBottom()
                );
                mImageList.get(0).getLocationInWindow(temp);
                Log.d(TAG, "onAnimationEnd: "+temp[0]+" "+temp[1]);
                TranslateAnimation translateAnimation = new TranslateAnimation(
                        0,
                        location_base_left[0]-axiosA[0],
                        0,
                        location_base_left[1]-axiosA[1]
                );
                translateAnimation.setInterpolator(new LinearInterpolator());
                translateAnimation.setFillEnabled(true);

                translateAnimation.setFillAfter(true);
                translateAnimation.setDuration(ANIMATION_RUNTIME/2);
                mImageList.get(0).startAnimation(translateAnimation);
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });
        Animation animationScale = new ScaleAnimation(
                1.0f,
                mViewSizeA[0]/mViewSizeB[0],
                1.0f,
                mViewSizeA[1]/mViewSizeB[1],
                ScaleAnimation.RELATIVE_TO_PARENT,
                ScaleAnimation.RELATIVE_TO_PARENT
        );

        Animation animationTrans = new TranslateAnimation(
                0,
                location[0]-location_base_left[0],
                0,
                location[1]-location_base_left[1]
        );

        set_2.addAnimation(animationScale);
        set_2.addAnimation(animationTrans);

        set_2.setInterpolator(new LinearInterpolator());
        set_2.setFillEnabled(true);

        set_2.setFillAfter(true);
        set_2.setDuration(ANIMATION_RUNTIME);
        mImageList.get(1).startAnimation(set_2);



//        AnimatorSet set = new AnimatorSet();

//        ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(
//                mImageList.get(1),
//                "scaleX",
//                1f,
//                mViewSizeA[0]/mViewSizeB[0]
//        );
//        set.playTogether(
//                objectAnimator,
//                ObjectAnimator.ofFloat(
//                        mImageList.get(1),
//                        "scaleY",
//                        1f,
//                        mViewSizeA[1]/mViewSizeB[1]
//                )
//        );
//        set.setDuration(4000);
//        set.start();



    }

    private void initParams() {
        mViewSizeA[0] = (float) mImageList.get(3).getWidth();
        mViewSizeA[1] = (float) mImageList.get(3).getHeight();
        mViewSizeB[0] = (float) mImageList.get(2).getWidth();
        mViewSizeB[1] = (float) mImageList.get(2).getHeight();

        for (int i=0;i<mViewSizeA.length;i++){
            Log.d(TAG, "AnimationHelper: "+mViewSizeA[i]+" "+mViewSizeB[i]);
        }


        // ------translate
        if(getView(R.id.guideline_hor_1)!=null){
            getView(R.id.guideline_hor_1).getLocationInWindow(guidelineY);
        }
        if(getView(R.id.guideline_ver_1)!=null){
            getView(R.id.guideline_ver_1).getLocationInWindow(guidelineX);
        }
        mImageList.get(3).getLocationInWindow(location);
        mImageList.get(1).getLocationInWindow(location_base_left);
        mImageList.get(2).getLocationInWindow(axiosA);

        Log.d(TAG, "onClick: location"+location[0]+" "+location[1]);
        Log.d(TAG, "onClick: location"+location_base_left[0]+" "+location_base_left[1]);
        Log.d(TAG, "onClick: X"+guidelineX[0]+" "+guidelineX[1]);
        Log.d(TAG, "onClick: Y"+guidelineY[0]+" "+guidelineY[1]);
    }

    private View getView(int id){
        View view = (View) mViewMap.get(""+id);
        Log.d(TAG, "getView: "+id+" "+view);
        return view;
    }


}
